home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 3876 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.4 KB

  1. Path: news.bridge.net!news
  2. From: David Byrden <100101.2547@compuserve.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: RE: Copt constructing an already default ....
  5. Date: 26 Jan 1996 14:22:32 GMT
  6. Organization: self-employed
  7. Message-ID: <4eao38$ab7@news.bridge.net>
  8. References: <4e9dfr$spc@dub-news-svc-6.compuserve.com>
  9. NNTP-Posting-Host: ppp-mia1-49.bridge.net
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 1.1N (Windows; I; 16bit)
  14.  
  15.  
  16. The lack of knowledge of good C++ practice displayed in the previous 
  17. post, is frightening.
  18.  
  19. >>> I've go an object which has already been constructed via its default
  20. >>> constructor which just sets all pointers to NULL.  What's the best
  21. >>> way to deep-copy into it ??
  22.  
  23. Firstly, the concept of shallow versus deep copying is not a solid 
  24. concept. Any given class may adopt a scheme for representing its data 
  25. which would make shallow copying illegal and deep copying hard to define.
  26.  
  27. If you make a default-constructed object X, and you then want to make it 
  28. a copy of another object Y, you ought to be able to do so like this;
  29.  
  30.      
  31.       Thing X;
  32.       X = Y ;
  33.  
  34. in other words, assignment should have the same END result as 
  35. constructing X from Y. You may choose to design a class where it does 
  36. not, but then you are making up your own rules for it, so I can't tell 
  37. you what is best.
  38.  
  39.  
  40. >>   A & operator= (const A & a )     { deepCopy( a );  return *this; }
  41.  
  42. Given the version of deepcopy() which is shown in the previous post, this 
  43. code will result in a memory leak almost every time an object of class A 
  44. is copied. The old memory is not deleted.
  45.  
  46.  
  47. >> It is not a good idea to memcpy() to an object.  If the object has no 
  48. >> virtual functions you will usually be OK
  49.  
  50. If the object has any pointers to internal parts of itself, if it 
  51. maintains any kind of reference or usage count, or if the copy 
  52. constructor or destructor or assignment operator have any other side 
  53. effects, or if it has base classes encapsulating their own (unknown, 
  54. changeable) behaviour, you will not be OK.
  55.  
  56.  
  57. >> but if the class does have virtual functions, you will most likely 
  58. >> over-write the virtual function table pointer and completely hose 
  59. >> yourself.
  60.  
  61. As a matter of fact, overwriting the virtual function table pointer is 
  62. one of the few things that will NOT "hose" you if you use memcpy() to 
  63. copy objects of the same class.
  64.  
  65.  
  66.                                David
  67.  
  68.  
  69.  
  70.